package com.geektime.congestion.wallet.service;

import com.geektime.congestion.wallet.dao.VirtualWalletDao;
import com.geektime.congestion.wallet.dao.WalletTransactionDao;
import com.geektime.congestion.wallet.domain.VirtualWallet;
import com.geektime.congestion.wallet.domain.WalletTransaction;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

public class VirtualWalletServiceImpl {

    private final VirtualWalletDao walletDao = new VirtualWalletDao();
    private final WalletTransactionDao walletTransactionDao = new WalletTransactionDao();

    // 充值
    public VirtualWallet add(String walletId, BigDecimal amount) {
        VirtualWallet wallet = walletDao.findByUuid(walletId);
        if (wallet == null) {
            wallet = new VirtualWallet(walletId, new Date());
        }
        wallet.add(amount);
        WalletTransaction walletTransaction = new WalletTransaction();
        String walletTransactionId = UUID.randomUUID().toString();
        walletTransaction.setUuid(walletTransactionId)
                .setWalletFromId(null)
                .setWalletToId(walletId)
                .setBalance(amount)
                .setCreateTime(new Date())
                .setType(1);
        walletTransactionDao.save(walletTransaction);
        return walletDao.save(wallet);
    }

    // 提现
    public VirtualWallet subtract(String walletId, BigDecimal amount) {
        VirtualWallet wallet = walletDao.findByUuid(walletId);
        if (wallet == null) {
            throw new IllegalArgumentException("walletId is not exist");
        }
        wallet.subtract(amount);
        WalletTransaction walletTransaction = new WalletTransaction();
        String walletTransactionId = UUID.randomUUID().toString();
        walletTransaction.setUuid(walletTransactionId)
                .setWalletFromId(walletId)
                .setWalletToId(null)
                .setBalance(amount)
                .setCreateTime(new Date())
                .setType(2);
        walletTransactionDao.save(walletTransaction);
        return walletDao.save(wallet);
    }

    // 转账
    public void transfer(String walletFromId, String walletToId, BigDecimal amount) {
        VirtualWallet walletFrom = walletDao.findByUuid(walletFromId);
        if (walletFrom == null) {
            throw new IllegalArgumentException("walletFromId is not exist");
        }
        VirtualWallet walletTo = walletDao.findByUuid(walletToId);
        if (walletTo == null) {
            throw new IllegalArgumentException("walletToId is not exist");
        }
        walletFrom.subtract(amount);
        walletTo.add(amount);
        walletDao.save(walletFrom);
        walletDao.save(walletTo);

        WalletTransaction walletTransaction = new WalletTransaction();
        String walletTransactionId = UUID.randomUUID().toString();
        walletTransaction.setUuid(walletTransactionId)
                .setWalletFromId(walletFromId)
                .setWalletToId(walletToId)
                .setBalance(amount)
                .setCreateTime(new Date())
                .setType(3);
        walletTransactionDao.save(walletTransaction);
    }

}
